package com.songshuai.webapi.crm.controller;

import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.crm.dto.request.clueimport.ClueImportReqDto;
import com.songshuai.webapi.crm.service.ClueImportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Api("线索导入")
@RestController
@RequestMapping("/api/clueImport/")
public class ClueImportController extends BaseController{

    @Autowired
    private ClueImportService clueImportService;

    /**
     * 获取模板文件下载地址
     */
    @ApiOperation(value = "获取模板文件下载地址")
    @RequestMapping(value = "getTemplateDownloadUrl", method = RequestMethod.POST)
    public QueryResponse getTemplateDownloadUrl() {
        try{
            return clueImportService.getTemplateDownloadUrl();
        } catch (Exception e) {
            log.error("api/clueImport/getTemplateDownloadUrl", e);
            return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED);
        }
    }

    /**
     * 数据校验
     */
    @ApiOperation(value = "数据校验")
    @RequestMapping(value = "uploadData", method = RequestMethod.POST)
    public QueryResponse uploadData(@RequestParam(name="file",required=false) MultipartFile file) {
        try{
            return clueImportService.uploadData(file);
        } catch (Exception e) {
            log.error("api/clueImport/uploadData", e);
            return new QueryResponse(ResultStatus.ERROR_FILE_UPLOAD);
        }
    }

    /**
     * 导入数据
     */
    @ApiOperation(value = "导入数据，持久化到数据库")
    @RequestMapping(value = "persistData", method = RequestMethod.POST)
    public QueryResponse persistData() {
        try{
            QueryResponse  queryResponse=clueImportService.persistData();
            return queryResponse;
        } catch (Exception e) {
            log.error("api/clueImport/persistData", e);
            return new QueryResponse(ResultStatus.ERROR_FILE_UPLOAD);
        }
    }

    @ApiOperation(value = "查询导入线索的分页列表")
    @PostMapping("queryClueImportList")
    public QueryResponse queryClueImportList(@RequestBody BaseParmDto<ClueImportReqDto> parmDto){
        try {
            QueryResponse  queryResponse=clueImportService.queryClueImportList(parmDto);
            return queryResponse;
        } catch (Exception e) {
            log.error("api/clueImport/queryClueImportList",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
    }






}
